package com.nf.action;

import com.nf.entity.Account;
import com.nf.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import java.util.Date;

@SessionAttributes("account")
@Controller
public class AccountAction {
    @Autowired
    private AccountService accountService;

    @ModelAttribute("account")
    Account getAccount() {
        return null;
    }
    //主页
    @RequestMapping("/index")
    public String index(@ModelAttribute("account")Account account,Model model){
        return "index";
    }

   //查询
    @RequestMapping("/queryBanlance")
    public String queryBanlance(@ModelAttribute("account") Account account,Model model){
        model.addAttribute("banlance",account.getBanLance());
        accountService.queryBanlanceById(account.getId());
        return "mybanlance";
    }
    //修改密码界面
    @RequestMapping("/updatePassword")
    public String updatepassword(@ModelAttribute("account")Account account,Model model){
        return "updatePassword";
    }
    //修改界面业务
    @RequestMapping("/updatePasswordgo")
    public String updatePasswordgo(@ModelAttribute("account")Account account,Model model,String oldPassword,String newPassword,String newPassword1){
        if(newPassword.equals(oldPassword)){
            model.addAttribute("message","OLD_EQUAL_NEW");
            return "redirect:updatePassword";
        }
        if (!newPassword.equals(newPassword1)){
            return "redirect:updatePassword";
        }
        accountService.updatePassword(newPassword,oldPassword , account.getId());
        return "ok";
    }
    //取钱界面
    @RequestMapping("/getmoney")
    public String getMoney(@ModelAttribute("account")Account account,Model model){
        return "getmoney";
    }
    //取钱业务
    @RequestMapping("/getMoneygo")
    public String getMoneygo(@ModelAttribute("account")Account account,Model model,Integer money){
        accountService.getMoney(money,account.getId());
        return "ok";
    }
    //存钱界面
    @RequestMapping("/savemoney")
    public String savemoney(@ModelAttribute("account")Account account,Model model){
        if(account==null){
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:Login";
        }
        return "SaveMoney";
    }
    //存钱业务
    @RequestMapping("/savemoneygo")
    public String savemoneygo(@ModelAttribute("account")Account account,Model model,Integer money){
        accountService.saveMoney(money,account.getId());
        return "ok";
    }
    //转账界面
    @RequestMapping("/transmoney")
    public String transmoney(@ModelAttribute("account")Account account,Model model){
        return "transMoney";
    }
    //转账业务
    @RequestMapping("/transmoneygo")
    public String transmoneygo(@ModelAttribute("account")Account account,Model model,Integer money,Integer UserIdDest){
        accountService.transMoney(money,UserIdDest,account.getId());
        return "ok";
    }
   }
